//
//  BMUserInfo.m
//  BicycleMap
//
//  Created by vedon on 5/15/16.
//  Copyright © 2016 Qianqian. All rights reserved.
//

#import "BMUserInfo.h"


@implementation BMUserInfo

- (instancetype)initWithUsername:(NSString *)name mobile:(NSString *)mobile location:(CLLocationCoordinate2D)location
{
    self = [super init];
    if (self)
    {
        [self configureWithUsername:name mobile:mobile location:location];
    }
    return self;
}

- (void)configureWithUsername:(NSString *)username mobile:(NSString *)mobile location:(CLLocationCoordinate2D)location
{
    self.username = username;
    self.mobile = mobile;
    self.location = [self locationToString:location];
}


- (void)setUserLocation:(CLLocationCoordinate2D)location
{
    self.location = [self locationToString:location];
}

- (CLLocationCoordinate2D)coordinate
{
    return [self stringToLocation:self.location];
}

#pragma mark - Private

- (NSString *)locationToString:(CLLocationCoordinate2D)location
{
    return [NSString stringWithFormat:@"%f,%f",location.latitude,location.longitude];
}

- (CLLocationCoordinate2D)stringToLocation:(NSString *)locationStr
{
    NSArray *component = [locationStr componentsSeparatedByString:@","];
    CLLocationCoordinate2D location = {0,0};
    if (component.count >= 2)
    {
        location.latitude = [[component objectAtIndex:0] floatValue];
        location.longitude = [[component objectAtIndex:1] floatValue];
    }
    return location;
}


@end
